#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>

#define voi2 290
#define voi1 220
#define JUMP_POWER_MAX 12.5



void text (SDL_Surface *ecran , char ch[50] ,SDL_Rect posback)
{ SDL_Surface *txt=NULL;
  TTF_Font *police = NULL;
  SDL_Color couleur = {0, 0, 0};
  SDL_Rect pos;
  pos.x=posback.x;
  pos.y=posback.y;
  TTF_Init();
  police = TTF_OpenFont("ComicPerso.ttf",80);
  txt = TTF_RenderText_Blended(police,ch, couleur);
  SDL_BlitSurface(txt, NULL, ecran, &pos); 
  //SDL_Flip(ecran);
  TTF_CloseFont(police);   
  TTF_Quit();
}

void apply_surface ( int x, int y, SDL_Surface* source, SDL_Surface* destination ){
 
     SDL_Rect offset;
     offset.x = x;
     offset.y = y;
     SDL_BlitSurface( source, NULL, destination, &offset );
}

void clean(SDL_Surface* ecran,SDL_Surface* back,SDL_Surface* perso,SDL_Surface* voit,SDL_Surface* voit1,SDL_Surface* bus,SDL_Surface* tube,SDL_Surface* moto,SDL_Surface* bat,SDL_Surface* batj,SDL_Surface* batr )
{
SDL_FreeSurface(back);    
SDL_FreeSurface(ecran);
SDL_FreeSurface(perso);
SDL_FreeSurface(voit);
SDL_FreeSurface(voit1);
SDL_FreeSurface(bus);
SDL_FreeSurface(tube);
SDL_FreeSurface(moto);
SDL_FreeSurface(bat);
SDL_FreeSurface(batr);
SDL_FreeSurface(batj);

}


	int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL,*back,*perso,*voit=NULL,*voit1=NULL,*bus=NULL,*tube=NULL,*moto=NULL,*bat=NULL,*batj=NULL,*batr=NULL,*bat1=NULL;
SDL_Init(SDL_INIT_VIDEO);
ecran=SDL_SetVideoMode(600,600, 32, SDL_HWSURFACE);
SDL_WM_SetCaption("Jeu (y)", NULL);
back=IMG_Load("90.png");
perso=IMG_Load("perso.bmp");
voit=IMG_Load("obs11.bmp");//voitpolice
voit1=IMG_Load("obs3.bmp");//voitrouge
bus=IMG_Load("bus.bmp");//bus
moto=IMG_Load("persoo.bmp");//moto
bat=IMG_Load("bat.bmp");//batt
batr=IMG_Load("batr.bmp");//batt rouge
batj=IMG_Load("batj.bmp"); //batt jaune


SDL_Rect posback,posperso,posvoit,posvoit1,posbat,postube,posmoto;
SDL_Event event;
int o=1,x,power=0,jump=0;
int bgx,bgy=150;
char ch[50];
int vit=5,l=0,l1=0,l2=0;
posperso.x=90;//perso
posperso.y=voi1;//perso
posbat.x=70;
posbat.y=150;

apply_surface(0,150,back,ecran);
SDL_SetColorKey(perso, SDL_SRCCOLORKEY, SDL_MapRGB(perso->format, 255, 255, 255));
SDL_SetColorKey(voit, SDL_SRCCOLORKEY, SDL_MapRGB(voit->format, 255, 255, 255));
SDL_SetColorKey(voit1, SDL_SRCCOLORKEY, SDL_MapRGB(voit1->format, 255, 255, 255));
SDL_SetColorKey(bus, SDL_SRCCOLORKEY, SDL_MapRGB(bus->format, 255, 255, 255));
SDL_SetColorKey(moto, SDL_SRCCOLORKEY, SDL_MapRGB(moto->format, 255, 255, 255));
//SDL_Flip(ecran);

bat1=bat;
while(o)
{




  
//scrolling
      
	    bgx -=vit;  
        x+=vit;
    

	    if( bgx <= -back->w )
       	 {  bgx = 0; }
//
        //jump
  if(event.type == SDL_KEYDOWN)

     if(event.key.keysym.sym == SDLK_SPACE)  
     
        if(posperso.y+posperso.h >= 300)
        {
            
            jump = 1;
            power = JUMP_POWER_MAX;
        }


        if(jump)
        {

            posperso.y -= power;

            power--;


            if(power == 0)
            jump = 0;
        
        }

        


        else if(posperso.y+posperso.h < 300)
        {

            posperso.y += power;
            
            if(power < JUMP_POWER_MAX)
            power++;
        }
//
     // collision 

       	if(((posperso.x+perso->w)==(bgx+940))&&(posperso.y==voi1))
       	{
       		SDL_Delay(1000);
       		o=0;
       	}
        if(((posperso.x+perso->w)==(bgx+1540))&&(posperso.y==voi1))
        {
            SDL_Delay(1000);
            o=0;    
        }
        if(((posperso.x+perso->w)==(bgx+1840))&&(posperso.y==voi2))
        {
            SDL_Delay(1000);
            o=0;
        }

        if(((posperso.x+perso->w)==(bgx+2540))&&(posperso.y==voi1))
        {
            SDL_Delay(1000);
            o=0;
        }
        if(((posperso.x+perso->w)==(bgx+3540))  &&(posperso.y==voi1))
         {
            SDL_Delay(1000);
            o=0;
        }
        if(((posperso.x+perso->w)==(bgx+500))&&(posperso.y==voi1))//col bat1
        {
                
                l=1;
               
        }
        if(((posperso.x+perso->w)==(bgx+1300))&&(posperso.y==voi2))//col bat2
        {
                
                l1=1;
               
        }

        if((l==0)&&(bgx<-500))
          {bat1=batj;}

         if(bgx<-1300)
          {if ((l1==0)&&(l==0))
            {bat1=batr;} 
            if((l==1)&&(l1==0))
            {bat1=batj;}
          if((l1==1)&&(l==1))
            {bat1=bat;}
          } 
        
    
        apply_surface( bgx, bgy, back, ecran );
        apply_surface( (bgx + back->w), bgy, back, ecran );
        SDL_BlitSurface(perso,NULL,ecran,&posperso);
        SDL_BlitSurface(bat1,NULL,ecran,&posbat);//blit score
      

        apply_surface( bgx+897, voi1, voit, ecran );
        apply_surface( bgx+1497, voi1, voit1, ecran );
        apply_surface( bgx+1797, voi2, bus, ecran );
        apply_surface( bgx+2497, voi1, moto, ecran );
        apply_surface( bgx+3497, voi1, tube, ecran );
        apply_surface( bgx+500, voi1, bat, ecran );//batterie colision
        apply_surface( bgx+1300, voi2, batr, ecran );//batterie colision2



	    
	   SDL_PollEvent(&event);
	switch(event.type)
			{
				case SDL_QUIT:
			o=0;
			break;
				case SDL_KEYDOWN:
            switch (event.key.keysym.sym)
            {
              
                 case SDLK_ESCAPE:
                 o=0;
                 	break;

                 case SDLK_UP:
					if(posperso.y!=voi1)
						posperso.y=voi1;

		                SDL_BlitSurface(perso,NULL,ecran,&posperso);

						break;
					case SDLK_DOWN:
						if(posperso.y!=voi2)
						posperso.y=voi2;

		                SDL_BlitSurface(perso,NULL,ecran,&posperso);
							
						break;	


                 
			     }

       	    }
	    
        


		
SDL_Delay(10);
SDL_Flip(ecran);


}
SDL_Quit();
return 0;
clean(ecran,back,perso,voit,voit1,bus,tube,moto,bat,batj,batr);
}
